<template>
  <div class="app">
    <div class="container">
      <!-- 头部 -->
      <mt-header fixed :title="$route.meta.title">
        <div v-show="show" @click="goBack" slot="left">
          <mt-button icon="back">返回</mt-button>
        </div>
      </mt-header>

      <!-- 身体部分 -->
      <transition name="fade">
        <router-view />
      </transition>

      <!-- 底部菜单 -->
      <tabber></tabber>
    </div>
  </div>
</template>

<script>
import tabber from './components/tabber'

export default {
  components: { tabber },
  data() {
    return {
      show: false
    }
  },
  created() {
    this.show = this.$route.path !== '/home'
    this.checkLogin()
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    // 检查是否已经登录
    checkLogin() {
      // 点击的任何一个页面都会显示在App.vue中 所以在这里再次检查请求头里的session_id 就可以应用到每个页面上了
      this.$http.get('user').then(res => {
        if (res.data.code === 1) {
          // 用户已经登录了，那就将用户的信息保存到vuex中
          this.$store.commit('user/setUser', res.data.data)
        }
      })
    }
  },
  watch: {
    '$route.path'(newVal) {
      // console.log(newVal);
      this.show = newVal !== '/home'
    }
  }
}
</script>

<style lang="scss">
* {
  touch-action:pan-y;
  padding: 0;
  margin: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  box-sizing: border-box;
}
.container {
  padding-top: 40px;
  padding-bottom: 50px;
  overflow: hidden;
}
.fade-enter {
  opacity: 0;
  transform: translateX(100%);
}
.fade-leave-to {
  opacity: 0;
  transform: translateX(-100%);
  position: absolute;
}
.fade-enter-active, .fade-leave-active {
  transition: all .5s ease;
}

</style>
